package com.xiaoc.bulldozer.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.xc.framework.common.exception.BusinessException;
import com.xc.framework.web.CookieUtils;
import com.xc.framework.web.FrontBaseController;
import com.xiaoc.bulldozer.common.Constant;
import com.xiaoc.bulldozer.common.dto.LoginUserDto;
import com.xiaoc.bulldozer.facade.UserFacade;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AdminBaseController extends FrontBaseController {

    private static final Logger logger = LoggerFactory.getLogger(AdminBaseController.class);

    @Resource
    private UserFacade userFacade;

    /**
     * 获取当前登录用户 ~
     * 
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected LoginUserDto getCurrentLoginUser(HttpServletRequest request, HttpServletResponse response) {

        String sessionId = CookieUtils.getCookieValue(getRequest(), Constant.COOKIE_SESSION_ID);
        if (StringUtils.isEmpty(sessionId)) {
            super.redirectUrl(request, response, getRequest().getContextPath() + "/gotoLogin");
            return null;
        }

        LoginUserDto loginUserDto = userFacade.getCurrentLoginUser(sessionId);
        if (loginUserDto == null) {
            super.redirectUrl(request, response, getRequest().getContextPath() + "/gotoLogin");
            return null;
        }
        
        return loginUserDto;
    }

    public LoginUserDto getCurrentLoginUser() {
        String sessionId = CookieUtils.getCookieValue(getRequest(), Constant.COOKIE_SESSION_ID);
        if (StringUtils.isEmpty(sessionId)) {
            return null;
        }

        LoginUserDto loginUserDto = userFacade.getCurrentLoginUser(sessionId);
        if (loginUserDto == null) {
            return null;
        }
        return loginUserDto;

    }
    /**
     * 异常拦截
     * @param e
     */
    @ExceptionHandler({Exception.class})
    public void exception(HttpServletRequest request, HttpServletResponse response, Exception e) {

        logger.error("exception : " + e.getMessage(), e);

        if (e instanceof BusinessException) {
            forward(request, response, request.getContextPath() + "/common/exception?msg=" + e.getMessage());
            return;
        }

        forward(request, response, getRequest().getContextPath() + "/common/exception?msg=" + e.getMessage());
    }

    protected String getDomainPath() {
        return getRequest().getScheme() + "://" + getRequest().getHeader("Host") + getRequest().getContextPath();
    }

    protected void loggerRequestBody(Object object, HttpServletResponse response) {

        try {
            Gson gson = new GsonBuilder().serializeNulls().create();
            LoginUserDto loginUserDto = getCurrentLoginUser(getRequest(), response);
            logger.info("userName:" + loginUserDto.getLoginName() + ", do:" + getRequest().getRequestURI() + ", data:" + gson.toJson(object));
        } catch (Exception e) {
            logger.error("loggerRequestBody failed", e);
        }
    }

}
